<?php
/**
 * HCOrderProcess.php
 * description:
 * Created by www.
 * User: SCF
 * Date: 2018/9/13 14:06
 *
 */

namespace App\Console\Commands\ThirdOrder;


use App\Services\ThirdAPI\HCAPIService;

use App\Services\ThirdAPI\GameApiService;
use App\Services\UserService;
use common\enum\CommonEnum;
use common\utils\RedisKeys;
use Illuminate\Support\Facades\Redis;

class HCOrderProcessor extends BaseOrderProcessor
{
    protected $signature = self::PREFIX . 'hc';
    protected $description = 'HC电竞注单拉取';

    // 电竞注单  51:无效单 52:未审核 53:未通过 54:已审核 55:系统无 效单 56: 中奖 57:无赛果 58:未中奖
    /*const HC_ORDER_INVALID = 51;
    const HC_ORDER_UN_CHECK = 52;
    const HC_ORDER_NO_PASS = 53;
    const HC_ORDER_CHECKED = 54;
    const HC_ORDER_SYSTEM_INVALID = 55;
    const HC_ORDER_WON = 56;
    const HC_ORDER_NO_RESULT = 57;
    const HC_ORDER_LOST = 58;*/

    public $hcService;
    public $redis;
    public $lockRedisKeyTime = RedisKeys::HC_LOCK_AUTOFIX_TIME;
    public $lockRedisKeyId = RedisKeys::HC_LOCK_AUTOFIX_ID;
    public $updateRedisKey= RedisKeys::HC_UPDATE_ID;
    public $latestUpdateKeyId;

    /**
     * 初始化信息，子类需要重写这个方法
     */
    public function init($argv = null)
    {
        // 必须调用父类init，初始化公共信息
        parent::init();
        // 5.第三方 partner id，名字，对应第三方表id
        $this->partnerName = "HC";
        $this->hcService = new HCAPIService();
        $this->redis = Redis::connection();
        $info = GameApiService::getApiPartnerInfoByName($this->partnerName);
        $this->partnerID = $info['id'];
        $this->orderType = self::ORDER_TYPE_ESPORT;
        $this->reportOrderType = self::GAME_TYPE_ESPORT;
        $this->userInfos = [];
    }

    /**
     * 向第三方API抓取数据
     */
    public function catchData()
    {
        $lastid = OrderStrategy::getUpdateKeyId($this->lockRedisKeyTime, $this->updateRedisKey,$this->lockRedisKeyId);
        $data = $this->hcService->bets((int)$lastid);
        if ($data) {
            $this->redis->set(RedisKeys::HC_UPDATE_ID,$data['next_sync_version']);
            $this->latestUpdateKeyId = $data['next_sync_version'];
            return $data['data'];
        } else {
            return [];
        }
    }

    /**
     * 处理数据
     * 不可以放复杂操作，因为拉取到的每个数据都会经过 load 加载
     */
    public function load($data)
    {
        $this->updateTime = $data['update_time'];
        $this->betTime = $data['bet_time'];
        $this->settleTime = $data['settle_time'];
        $this->reportTime = $data['report_time'];
        $this->betMoney = $data['bet_money'];
        $this->prizeMoney = $data['prize_money'];
        $this->winLossMoney = $data['winloss_money'];
        $this->apiUserName = $data['api_username'];
        $this->contributeMoney = $data['contribute_money']??0;
        $this->bonusMoney = $data['bonus_money']??0;
        $this->validMoney = $data['valid_money'];
        $this->commonStatus = $data['common_status'];
        $this->updateKey = $data['update_key'];
        $this->apiStatus = $data['api_status'];
        $this->gameCode = $data['game_code'];
        $this->gameName = $data['game_name'];
        $this->betContent = $data['bet_content'];
        $this->originData = $data['origin_data'];
        $this->orderNumber = $data["order_number"];

    }
    /**
     * 处理数据，每个子类需要根据第三方的单条数据内容来获取有用的信息
     */
    public function initFromData($data)
    {
    }
    /*
     * 更新redis拉取注单ID
     */
    public function updateFlag($updateKey){
        // 更新 version_key
        $lastid = $this->redis->get(RedisKeys::HC_UPDATE_ID);
        $lastid = $updateKey > $lastid ? $updateKey : $lastid;
        $this->redis->set(RedisKeys::HC_UPDATE_ID,$lastid);
    }
    /**
     * 修复数据内容，每个子类需要实现怎么修复数据内容
     */
    public function fixData($start_date)
    {
    }
}